package com.meitianhui.convenitenbuy.module.store.adapter;

import android.support.annotation.Nullable;
import android.text.TextUtils;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.meitianhui.convenitenbuy.R;
import com.meitianhui.convenitenbuy.module.store.model.GroupSaleInfo;
import com.meitianhui.convenitenbuy.util.GlideUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/11/27.
 */

public class GroupSaleAdapter extends BaseQuickAdapter<GroupSaleInfo.ListBean,BaseViewHolder> {
    private Map<String, String> mUrlMap;
    public GroupSaleAdapter(@Nullable List<GroupSaleInfo.ListBean> data) {
        super(R.layout.item_group_sale, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, GroupSaleInfo.ListBean item) {
        String goods_pic_info= item.getGoods_pic_info();
        String url="";
        try {
            JSONArray jsonArray=new JSONArray(goods_pic_info);
            for(int i=0;i<jsonArray.length();i++){
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                String pid = jsonObject.getString("path_id");
                if (!TextUtils.isEmpty(pid)) {
                    url = pid;
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        GlideUtils.setRectImage(mContext,mUrlMap.get(url),R.mipmap.icon_default_picture,R.mipmap.icon_default_picture,helper.getView(R.id.iv_feature));
        helper.setText(R.id.tv_title,item.getGoods_title());
        helper.setText(R.id.tv_price,"¥" +item.getRetail_price());
        helper.setText(R.id.tv_sale,"已售 "+item.getSub_order_qty());
        int i_Sub_order_qty=0;
        int i_Qty_limit=1;
        try{
            i_Sub_order_qty=Integer.parseInt(item.getSub_order_qty());
            i_Qty_limit=Integer.parseInt(item.getQty_limit());
            double i_per=(double)i_Sub_order_qty/(double)i_Qty_limit;
            if(i_per>1){
                i_per=1;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        int ix=i_Qty_limit-i_Sub_order_qty;
        if(ix<0){
            ix=0;
        }
        helper.setText(R.id.tv_need,"还需 " + ix);
    }

    public void setNewData(List data, Map<String, String> map) {
        setNewData(data);
        this.mUrlMap = map;
    }
}
